package ModelLayer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.*;
/**
 * Write a description of class LoanContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanContainer
{
    private ArrayList<Loan> loanList;
    private String startResult;
    private String finishResult;
    private DVDContainer dvd;
    private PersonContainer person;
    private static LoanContainer instance;
    private LoanContainer()
    {
        loanList = new ArrayList<Loan>();
        dvd = DVDContainer.getInstance();
        person = PersonContainer.getInstance();
    }
    
    public static LoanContainer getInstance()
    {
        if (instance == null)
        {
            instance= new LoanContainer();
        }
        return instance;
    }
    
    public void addLoans(Loan loan)
    {
        int y = 0;
        int y1 = 0;
        boolean found1 = false;
        boolean found2 = false;
        while(y < dvd.getDVDList().size() && !found1)
        {
            if(dvd.getDVDList().size() > 0)
            {
                y1 = 0;
                while(y1 < dvd.getDVDList().get(y).getDvdCopyList().size() && !found2)
                {
                    if(dvd.getDVDList().get(y).getDvdCopyList().get(y1).getSerialNumber().equals(loan.getSerialNumber()) && 
                    dvd.getDVDList().get(y).getDvdCopyList().get(y1).getState().equals("Copy is available."))
                    {
                        found1 = true;
                        found2 = true;
                    }
                    else
                    {
                        y1++;
                    }
                }
            }
            if(found1 == false)
            {
                y++;
            }
        }
        int i =0;
        boolean found3 = false;
        while(i < person.getPersonList().size() && !found3)
        {
            if(person.getPersonList().get(i).getId().equals(loan.getPersonID()))
            {
                found3 = true;
            }
            else
            {
                i++;
            }
        }
        int a = 0;
        boolean found4 = false;
        while(a < loanList.size() && !found4)
        {
            if(loan.getId().equals(loanList.get(a).getId()))
            {
                found4 = true;
            }
            else
            {
                a++;
            }
        }
        if(found2 == true && found3 == true && found4 == false)
        {
            dvd.getDVDList().get(y).getDvdCopyList().get(y1).setState(false);
            loanList.add(loan);
            startResult ="A loan started.";
        }
        else
        {
            startResult ="\nInappropirate values. \nMaybe there is already a loan with this ID. \nPerson's ID, copy's serial number may not be correct. \nOr maybe the copy is not available at the moment.";
        }
    }
    
    public String startResult()
    {
        return startResult;
    }
    
    public void endLoans(String loanID, String currentDate)
    {
        String serial="";
        String borrowDate="";
        int period = 0;
        int index = 0;
        boolean found = false;
        while(index < loanList.size() && !found)
        {
            if(loanList.get(index).getId().equals(loanID))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            serial = loanList.get(index).getSerialNumber();
            borrowDate = loanList.get(index).getBorrowDate();
            period = period + loanList.get(index).getPeriod();
        }   
        int i = 0;
        int y =0;
        boolean found1 = false;
        boolean found2 = false;
        while( i < dvd.getDVDList().size() && !found1)
        {
            if(dvd.getDVDList().size() > 0)
            {
                y = 0;
                while( y < dvd.getDVDList().get(i).getDvdCopyList().size() && !found2)
                {
                    if(dvd.getDVDList().get(i).getDvdCopyList().get(y).getSerialNumber().equals(serial) && 
                    dvd.getDVDList().get(i).getDvdCopyList().get(y).getState().equals("Copy is not available."))
                    {
                        found1 = true;
                        found2 = true;
                    }
                    else
                    {
                        y++;
                    }
                }
            }
            if(found1 == false)
            {
                i++;
            }
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        boolean found3 = false;
        try
        {
            Date borrow = sdf.parse(borrowDate);
            //sdf.format(borrowDeit);
            Date current = sdf.parse(currentDate);
            
            Calendar c = Calendar.getInstance();
            c.setTime(borrow);
            c.add(Calendar.DATE, period);
            Date newDate = c.getTime();
            if(newDate.compareTo(current) > 0 || newDate.compareTo(current) == 0)
            {
                found3 = true;
            }
            else
            {
                found3 = false;
            }
        }
        catch (ParseException e)
        {
            
        }
       
        if(found && found2 == true && found3 == true)
        {
            dvd.getDVDList().get(i).getDvdCopyList().get(y).setState(true);
            finishResult="DVD returned.";
            loanList.remove(index);
        }
        else if(found && found2 == true && found3 == false)
        {
            dvd.getDVDList().get(i).getDvdCopyList().get(y).setState(true);
            finishResult="DVD returned. Fine: 50$!";
            loanList.remove(index);
        }
        else
        {
            finishResult="Wrong loan ID.";
        }
    }
    
    public String finishResult()
    {
        return finishResult;
    }
    
    public String showLoans(String ID)
    {
        String loan="";
        int index = 0;
        boolean found = false;
        while(index < loanList.size() && !found)
        {
            if(loanList.get(index).getId().equals(ID))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            loan =" Borrow date: " + loanList.get(index).getBorrowDate() + "\n Serian number: "
            + loanList.get(index).getSerialNumber() + "\n Period: "
            + loanList.get(index).getPeriod() + " days\n Borrower: "
            + loanList.get(index).getPerson() + "\n State: "
            + loanList.get(index).isLending();
        }
        else
        {
            loan = "No loan with that ID.";
        }
        return loan;
    } 
}